<template>
  <el-dialog
    title="选择计划单"
    :visible="selectImportMaterialDialog"
    width="70%"
    class="mo-order-select"
    @close="selectImportMaterialClose"
  >
    <div class="app-container">
      <div class="layout-box">
        <el-form :inline="true" label-width="70px" class="search-top">
          <div class="search-sub">
            <el-form-item label-width="82">
              <el-input
                v-model="queryParams.ppNumber"
                clearable
                size="small"
                placeholder="销售单号"
                style="width: 150px"
              />
            </el-form-item>
            <el-form-item label-width="82">
              <el-input
                v-model="queryParams.invCode"
                clearable
                size="small"
                placeholder="物料编码"
                style="width: 150px"
              />
            </el-form-item>
            <el-form-item label-width="82">
              <el-input
                v-model="queryParams.invName"
                clearable
                size="small"
                placeholder="物料名称"
                style="width: 200px"
              />
            </el-form-item>
            <el-form-item label-width="82">
              <el-date-picker
                v-model="valueDate"
                type="daterange"
                align="right"
                unlink-panels
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :picker-options="pickerOptions"
                style="width: 250px"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="handleSearch"
                >搜索
              </el-button>
            </el-form-item>
          </div>
          <div>
            <el-button
              type="primary"
              plain
              icon="el-icon-menu"
              size="mini"
              @click="checkleAll"
              >查询所有</el-button
            >
          </div>
        </el-form>
        <!-- 单据列表 -->
        <el-table
          :data="billTableData"
          style="width: 100%"
          v-loading="loading"
          @row-click="clickRowRadio"
          :height="maxHeight"
          border
          ref="billTable"
        >
          <el-table-column
            prop="pp_number"
            label="销售单号"
            align="center"
            width="120"
          >
          </el-table-column>
          <el-table-column
            prop="pp_date"
            label="订单交期"
            align="center"
            width="110"
          >
            <template v-slot="scope">
              <span>
                {{ scope.row.pp_date | inMaterTime }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            prop="mp_order_code"
            label="计划单号"
            align="center"
            width="120"
          >
          </el-table-column>

          <el-table-column
            prop="inv_code"
            label="物料编码"
            align="center"
            width="120"
          >
          </el-table-column>
          <el-table-column label="物料名称" align="center" width="280">
            <template v-slot="scope">
              <div>
                <span
                  >{{ scope.row.inv_name }} {{ scope.row.inv_attribute }}</span
                >
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="unit_name"
            label="主计量"
            align="center"
            width="60"
          >
          </el-table-column>
          <el-table-column prop="surplus" label="计划数量" align="center">
            <template v-slot="scope">
              <div style="text-align: right">
                <span>{{ scope.row.inv_quantity }} </span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="surplus" label="已生产量" align="center">
            <template v-slot="scope">
              <div style="text-align: right">
                <span>{{ scope.row.wi_quantity }} </span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="surplus" label="未生产量" align="center">
            <template v-slot="scope">
              <div style="text-align: right">
                <span>{{ scope.row.surplus }} </span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="need_date"
            label="需求日期"
            align="center"
            width="100"
          >
            <template v-slot="scope">
              <span
                v-if="
                  scope.row.prs_date !== null &&
                  scope.row.prs_date !== undefined
                "
                >{{ scope.row.prs_date | inMaterTime }}</span
              >
            </template>
          </el-table-column>

          <el-table-column width="50" align="center">
            <template v-slot="scope">
              <el-radio
                v-model="successData"
                :label="scope.row"
                @change="radioData(scope.row)"
              >
                选择
              </el-radio>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <pagination
        v-if="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getBillList"
      />
    </div>
    <div class="ImportMaterial-success">
      <div>
        <el-button
          :disabled="successStatus"
          type="primary"
          @click="handleSuccess"
          >确定</el-button
        >
        <el-button @click="handleClose">取消</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { listImportBill } from "../../api/order";
let self;
export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      detailLoding: true,
      //查询单据参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        ppNumber: "",
        invCode: "",
        invName: "",
        startDate: "",
        endDate: "",
      },
      // 开始时间/结束时间配置
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date(
                new Date(new Date().toLocaleDateString()).getTime()
              );
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date(
                new Date(new Date().toLocaleDateString()).getTime()
              );
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date(
                new Date(new Date().toLocaleDateString()).getTime()
              );
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      valueDate: [],
      // 总条数
      total: 0,
      // 最大高度
      maxHeight: null,
      //单据数据
      billTableData: [],
      //已选单据
      successData: null,
      successStatus: true,
    };
  },
  props: {
    selectImportMaterialDialog: {
      type: Boolean,
      default: true,
    },
    // 小数位数
    decimal: {
      type: String,
      default: "2",
    },
  },
  beforeCreate() {
    self = this;
  },
  created() {
    this.getBillList();
  },
  methods: {
    //查询所有
    checkleAll() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 100,
        ppNumber: "",
        invCode: "",
        invName: "",
      };
      this.valueDate = [];
      this.successStatus = true;
      this.getBillList();
    },

    // 获取单据
    getBillList() {
      this.loading = true;
      listImportBill(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.loading = false;
          this.billTableData = res.rows;
          this.getDecimal();
          this.total = res.total;
          this.maxHeight = `${document.documentElement.clientHeight}` - 390;
        }
      });
    },

    // 搜索
    handleSearch() {
      // 日期转换
      if (this.valueDate !== undefined &&
        this.valueDate !== null &&
        this.valueDate.length > 0) {
        this.queryParams.startDate = this.parseTime(this.valueDate[0]);
        this.queryParams.endDate = this.setTime(this.valueDate[1]);
      } else {
        this.queryParams.startDate = "";
        this.queryParams.endDate = "";
      }
      this.getBillList();
    },

    //格式化数量单价金额小数位数
    getDecimal() {
      if (this.billTableData.length > 0) {
        this.billTableData.forEach((item) => {
          if (item.inv_quantity !== null && item.inv_quantity !== undefined) {
            item.inv_quantity = parseFloat(item.inv_quantity).toFixed(
              this.decimal
            );
          }
          if (item.wi_quantity !== null && item.wi_quantity !== undefined) {
            item.wi_quantity = parseFloat(item.wi_quantity).toFixed(
              this.decimal
            );
          }
          if (item.surplus !== null && item.surplus !== undefined) {
            item.surplus = parseFloat(item.surplus).toFixed(this.decimal);
          }
        });
      }
    },

    // 弹框状态
    selectImportMaterialClose() {
      this.$emit("selectImportMaterialClose");
    },

    //单选
    radioData(data) {
      this.successStatus = false;
      this.successData = data;
    },

    //确定选取
    handleSuccess() {
      this.$emit("handleImportMaterialSuccess", this.successData);
      this.selectImportMaterialClose();
    },

    // 关闭弹框
    handleClose() {
      this.selectImportMaterialClose();
    },

    //点击中子
    clickRowRadio(row) {
      this.radioData(row);
    },

    //时间+23时59分59秒
    setTime(date) {
      date.setHours(date.getHours() + 23);
      date.setMinutes(date.getMinutes() + 59);
      date.setSeconds(date.getSeconds() + 59);
      let time = new Date(date);
      return (
        time.getFullYear() +
        "-" +
        (time.getMonth() + 1) +
        "-" +
        time.getDate() +
        " " +
        time.getHours() +
        ":" +
        time.getMinutes() +
        ":" +
        time.getSeconds()
      );
    },
  },
  filters: {
    //时间格式化
    inMaterTime(date) {
      let time = new Date(date);
      return (
        time.getFullYear() + "-" + (time.getMonth() + 1) + "-" + time.getDate()
      );
    },

    //交期状态
    filterStatus(status) {
      switch (status) {
        case "0":
          return "预计";
          break;
        case "1":
          return "确认";
          break;
        default:
          return "";
      }
    },

    //匹配用料方式
    matchingInvType(data) {
      let supply = "";
      if (self.optionsInvInType && self.optionsInvInType.length > 0) {
        self.optionsInvInType.forEach((item) => {
          if (item.dictValue == data) {
            supply = item.dictLabel;
          }
        });
      }
      return supply;
    },
  },
};
</script>
<style lang="scss">
.mo-order-select {
  .el-dialog__body {
    height: calc(100vh - 250px);
    position: relative;
  }
  .el-radio__label {
    display: none;
  }
  .el-range-editor--medium.el-input__inner {
    line-height: 32px;
  }
  .el-range-editor--medium .el-range__icon,
  .el-range-editor--medium .el-range__close-icon,
  .el-range-editor--medium .el-range-separator {
    line-height: 24px;
  }
  .el-range-editor--medium.el-input__inner {
    height: 32px;
  }
  .el-table--border {
    border-top: none;
  }
  .parent-table {
    th,
    td {
      border-right: 1px solid #dfe6ec;
    }
    th:nth-of-type(1),
    td:nth-of-type(1) {
      border-left: 1px solid #dfe6ec;
    }
  }
  .child-table {
    margin-bottom: 24.5px;
    margin-top: -5.5px;
    box-sizing: border-box;
  }
  .search-top {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
  }

  .el-pagination {
    left: 0;
  }

  .el-table__header-wrapper {
    .el-checkbox {
      display: none;
    }
  }
  .app-container {
    padding-top: 0;
  }

  .layout-box {
    .el-link.is-underline:hover:after {
      border: none;
    }

    .search-box {
      display: flex;
      margin-bottom: 20px;

      .search-input {
        display: flex;
        margin-right: 15px;

        .input-data {
          min-width: 220px !important;
        }

        .el-input {
          margin-right: 15px;
        }
      }
    }

    .sort-active {
      background: #1890ff !important;
      border-color: #1890ff !important;
      color: #ffffff !important;
    }

    .con-top {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }

    .material-sort {
      span {
        font-size: 14px;
        padding: 0px 15px;
        background: #f4f4f5;
        color: #999;
        display: inline-block;
        border-radius: 3px;
        margin-left: 5px;
        margin-right: 5px;
        cursor: pointer;
        line-height: 26px;
        border: 1px solid #e9e9eb;
      }
    }
  }

  .ImportMaterial-success {
    position: absolute;
    bottom: 20px;
    right: 40px;

    button {
      padding: 8px 40px;
    }
  }
}
</style>
